﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;


namespace geetol.sms.core
{
    public class Secret
    {
        #region AES
        private static byte[] AesKeys = new byte[] { 0x41, 0x72, 0x65, 0x79, 0x6f, 0x75, 0x6d, 0x79, 0x53, 110, 0x6f, 0x77, 0x6d, 0x61, 110, 0x3f };

        public static string AesDecode(string input, string key)
        {
            return Encoding.UTF8.GetString(AesDecode(Encoding.UTF8.GetBytes(input), key));
        }
        public static byte[] AesDecode(byte[] data, string decryptKey)
        {
            if (data == null) return null;

            try
            {
                decryptKey = decryptKey.Substring(0, 0x20);
                decryptKey = decryptKey.PadRight(0x20, ' ');
                return new RijndaelManaged { Key = Encoding.UTF8.GetBytes(decryptKey), IV = AesKeys }.CreateDecryptor().TransformFinalBlock(data, 0, data.Length);
            }
            catch
            {
                return null;
            }
        }
        public static string AesEncode(string input, string key)
        {
            return Encoding.UTF8.GetString(AesEncode(Encoding.UTF8.GetBytes(input), key));
        }
        public static byte[] AesEncode(byte[] data, string encryptKey)
        {
            if (data == null) return null;
            try
            {
                encryptKey = encryptKey.Substring(0, 0x20);
                encryptKey = encryptKey.PadRight(0x20, ' ');
                return new RijndaelManaged { Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 0x20)), IV = AesKeys }.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region DES
        private static byte[] DesKeys = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
        public static string DesDecode(string decryptString, string decryptKey)
        {

            try
            {
                if (decryptString == string.Empty || decryptKey.Length < 8)
                {
                    decryptKey = DesKeys.ToString();
                }
                else
                {
                    decryptKey = decryptKey.Substring(0, 8);
                    decryptKey = decryptKey.PadRight(8, ' ');
                }
                byte[] bytes = Encoding.UTF8.GetBytes(decryptKey);
                byte[] keys = DesKeys;
                byte[] buffer = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, keys), CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                return Encoding.UTF8.GetString(stream.ToArray());
            }
            catch
            {
                return "";
            }
        }

        public static string DesEncode(string encryptString, string encryptKey)
        {
            encryptKey = encryptKey.Substring(0, 8);
            encryptKey = encryptKey.PadRight(8, ' ');
            byte[] bytes = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] keys = DesKeys;
            byte[] buffer = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(bytes, keys), CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            return Convert.ToBase64String(stream.ToArray());
        }


        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="eString"></param>
        /// <returns></returns>
        public static string DesEncrypt(string eString)
        {
            var b = Encoding.UTF8.GetBytes(eString);
            b = DesEncrypt(b);
            return Convert.ToBase64String(b);
        }
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="dString"></param>
        /// <returns></returns>
        public static string DesDecrypt(string dString)
        {
            var b = Convert.FromBase64String(dString);
            b = DesDecrypt(b);
            return Encoding.UTF8.GetString(b);
        }

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="encryptString"></param>
        /// <returns></returns>
        public static byte[] DesEncrypt(byte[] inputByteArray)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes("yonggenb");
            byte[] keyIV = Encoding.UTF8.GetBytes("jingxima");
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return mStream.ToArray();
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="decryptString"></param>
        /// <returns></returns>
        public static byte[] DesDecrypt(byte[] inputByteArray)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes("yonggenb");
            byte[] keyIV = Encoding.UTF8.GetBytes("jingxima");
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return mStream.ToArray();
        }



        #endregion

        #region Md5

        /// <summary>
        /// 获取数组的MD5码
        /// </summary>
        /// <param name="buff"></param>
        /// <returns></returns>
        public static string MD5(byte[] buff)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(buff);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        }

        /// <summary>
        /// MD5函数
        /// </summary>/// <param name="str">原始字符串</param>/// <returns>MD5结果</returns>
        public static string MD5(string str)
        {
            return MD5(str, 3);
        }

        public static string MD5(string str, int c)
        {
            return MD5(str, c, "zkmj_xcms_love");
        }

        public static string MD5(string str, int c, string sign)
        {
            if (c > 0) str += sign;
            if (string.IsNullOrEmpty(str)) return "";
            byte[] b = Encoding.UTF8.GetBytes(str);
            using (MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider())
            {
                b = mD5CryptoServiceProvider.ComputeHash(b);
            }
            string ret = "";
            for (int i = 0; i < b.Length; i++) ret += b[i].ToString("x").PadLeft(2, '0');
            c--;
            if (c <= 0) return ret;
            return MD5(ret, c, sign);
        }
        #endregion

        #region SHA
        /// <summary>
        /// SHA256函数
        /// </summary>
        /// /// <param name="str">原始字符串</param>/// <returns>SHA256结果</returns>
        public static string SHA256(string str)
        {
            byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
            SHA256Managed Sha256 = new SHA256Managed();
            byte[] Result = Sha256.ComputeHash(SHA256Data);
            return BitConverter.ToString(Result).Replace("-", "");
        }
        public static string SHA348(string str)
        {
            byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
            SHA384Managed Sha256 = new SHA384Managed();
            byte[] Result = Sha256.ComputeHash(SHA256Data);
            return BitConverter.ToString(Result).Replace("-", "");
        }
        public static string SHA512(string str)
        {
            byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
            SHA512Managed Sha256 = new SHA512Managed();
            byte[] Result = Sha256.ComputeHash(SHA256Data);
            return BitConverter.ToString(Result).Replace("-", "");
        }
        public static string SHA1(string str)
        {
            byte[] SHA256Data = Encoding.UTF8.GetBytes(str);
            SHA1Managed Sha256 = new SHA1Managed();
            byte[] Result = Sha256.ComputeHash(SHA256Data);
            return BitConverter.ToString(Result).Replace("-", "");
        }
        #endregion

        #region Base64
        public static string ToBase64(string text)
        {
            try
            {
                var bytes = Encoding.UTF8.GetBytes(text);
                return Convert.ToBase64String(bytes);
            }
            catch
            {
                return "";
            }
        }

        public static string FormBase64(string code)
        {
            try
            {
                var bytes = Convert.FromBase64String(code);
                return Encoding.UTF8.GetString(bytes);
            }
            catch { return ""; }
        }

        public static string ToUrlBase64(string text)
        {
            return ToBase64(text).Replace("+", ",").Replace("/", "|");
        }

        public static string FromUrlBase64(string text)
        {
            return FormBase64(text.Replace(",", "+").Replace("|", "/"));
        }
        #endregion
    }
}
